package cn.yunyichina.provider.pay.service;

import cn.yunyichina.provider.iface.entity.alipay.Alipay;
import cn.yunyichina.provider.iface.entity.alipay.AlipayFacepayMedicalCardQueryRequest;
import cn.yunyichina.provider.iface.entity.alipay.AlipayWebpayMedicalCardQueryRequest;
import cn.yunyichina.provider.pay.entity.PaymentSetting;
import cn.yunyichina.provider.pay.exception.PaySettingNotFoundException;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * Created by Lullaby on 2016/12/27.
 */
public class BaseMedicareService extends BaseService {

    protected boolean checkAlipayWebpayMedicalCardQueryParameters(Map<String, String> params) {
        if (CollectionUtils.isEmpty(params)) return false;
        if (StringUtils.isEmpty(params.get("app_id"))) return false;
        if (StringUtils.isEmpty(params.get("auth_code"))) return false;
        if (StringUtils.isEmpty(params.get("return_url"))) return false;
        return true;
    }

    protected AlipayWebpayMedicalCardQueryRequest buildAlipayWebpayMedicalCardQueryParameters(Map<String, String> params) throws PaySettingNotFoundException {
        PaymentSetting paymentSetting = this.getPaymentSetting(params.get("app_id"));
        String signType = "RSA";
        if (paymentSetting.getPayPublicKey().length() > 256) {
            signType = "RSA2";
        }

        AlipayWebpayMedicalCardQueryRequest queryRequest = new AlipayWebpayMedicalCardQueryRequest();
        queryRequest.setPublicKey(paymentSetting.getPublicKey());
        queryRequest.setPrivateKey(paymentSetting.getPrivateKey());

        queryRequest.setAppId(params.get("app_id"));
        queryRequest.setMethod("alipay.commerce.medical.card.query");
        queryRequest.setFormat("JSON");
        queryRequest.setCharset("utf-8");
        queryRequest.setSignType(signType);
        queryRequest.setTimestamp(DateUtils.getDatetime());
        queryRequest.setVersion("1.0");

        queryRequest.setSignFlag("true");

        queryRequest.setAuthCode(params.get("auth_code"));
        queryRequest.setReturnUrl(params.get("return_url"));
        queryRequest.setCardOrgNo("SZHRSS");

        return queryRequest;
    }

    protected boolean checkAlipayFacepayMedicalCardQueryParameters(Map<String, String> params) {
        if (CollectionUtils.isEmpty(params)) return false;
        if (StringUtils.isEmpty(params.get("app_id"))) return false;
        if (StringUtils.isEmpty(params.get("auth_code"))) return false;
        return true;
    }

    protected AlipayFacepayMedicalCardQueryRequest buildAlipayFacepayMedicalCardQueryParameters(Map<String, String> params) throws PaySettingNotFoundException {
        PaymentSetting paymentSetting = this.getPaymentSetting(params.get("app_id"));
        String signType = "RSA";
        if (paymentSetting.getPayPublicKey().length() > 256) {
            signType = "RSA2";
        }

        AlipayFacepayMedicalCardQueryRequest queryRequest = new AlipayFacepayMedicalCardQueryRequest();
        queryRequest.setPublicKey(paymentSetting.getPublicKey());
        queryRequest.setPrivateKey(paymentSetting.getPrivateKey());

        queryRequest.setAppId(params.get("app_id"));
        queryRequest.setMethod("alipay.commerce.medical.card.query");
        queryRequest.setFormat("JSON");
        queryRequest.setCharset("utf-8");
        queryRequest.setSignType(signType);
        queryRequest.setTimestamp(DateUtils.getDatetime());
        queryRequest.setVersion("1.0");

        AlipayFacepayMedicalCardQueryRequest.BizContent bizContent = new AlipayFacepayMedicalCardQueryRequest.BizContent();
        bizContent.setAuthCode(params.get("auth_code"));
        if (!StringUtils.isEmpty(params.get("scene")) && Alipay.Scene.WAVECODE.toString().equals(params.get("scene"))) {
            bizContent.setScene(Alipay.Scene.WAVECODE.getValue());
        } else {
            bizContent.setScene(Alipay.Scene.BARCODE.getValue());
        }
        bizContent.setCardOrgNo("SZHRSS");

        queryRequest.setBizContent(JsonUtils.toJsonString(bizContent));

        return queryRequest;
    }

}
